package com.travel.modules.asset.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.travel.commons.utils.PageUtils;
import com.travel.commons.utils.Query;
import com.travel.modules.asset.dao.RouteDao;
import com.travel.modules.asset.dto.RouteQueryDTO;
import com.travel.modules.asset.entity.Route;
import com.travel.modules.asset.service.RouteService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("routeService")
public class RouteServiceImpl extends ServiceImpl<RouteDao, Route> implements RouteService {
    @Resource
    private RouteDao routeDao;

    @Override
    public PageUtils getMatchGoods(RouteQueryDTO params) {
        String name = params.getName();
        String address = params.getAddress();
        Integer sort = params.getSort();

        Integer currPage = params.getPage();
        Integer limit = params.getLimit();
        Map<String, Object> pageMap = new HashMap<>();
        pageMap.put("page", currPage);
        pageMap.put("limit", limit);

        QueryWrapper<Route> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(name)) {
            queryWrapper.like("name", name);
        }
        if (StringUtils.isNotBlank(address)) {
            queryWrapper.like("address", address);
        }
        if (sort == null) {
            sort = 3;
        }
        switch (sort) {
            case 1: queryWrapper.orderByDesc("price"); break;
            case 2: queryWrapper.orderByDesc("reputation"); break;
            default: queryWrapper.orderByDesc("hot"); break;
        }

        IPage<Route> page = this.page(
                new Query<Route>().getPage(pageMap),
                queryWrapper
        );
        return new PageUtils(page);
    }

    @Override
    public List<Route> hotGoods() {
        return routeDao.getHotRoutes();
    }

    @Override
    public List<Route> ftQueryGoods(String[] keywords) {
        return routeDao.ftQueryRoutes(keywords);
    }

    @Override
    public List<Route> getAllRoute(Map<String, Object> params) {
        return routeDao.getAllRoute(params);
    }

    @Override
    public Route getOneRoute(String goodsId) {
        return routeDao.getOneRoute(goodsId);
    }
}
